package br.urca.impl;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import br.urca.dao.TotalAulasDAO;
import br.urca.model.TotalAulas;

@Transactional
public class JPATotalAulasDAO implements TotalAulasDAO {
	
	private EntityManager manager;
	
	public JPATotalAulasDAO() {
		this(null);
	}
	
	@Inject
	public JPATotalAulasDAO(EntityManager manager) {
		this.manager = manager;
	}

	@Override
	public List<TotalAulas> todos() {
		return this.manager.createQuery("SELECT t FROM TotalAulas t", TotalAulas.class)
				.getResultList();
	}

	@Override
	public TotalAulas getObject(String oferta, String pedido, 
			short ano, short mes) {
		try {
			return this.manager.createQuery("SELECT t FROM TotalAulas t "
					+ "WHERE t.id.codOferta = :oferta "
					+ "AND t.id.codPedido = :pedido "
					+ "AND t.id.ano = :ano "
					+ "AND t.id.mes = :mes ", TotalAulas.class)
					.setParameter("oferta", oferta)
					.setParameter("pedido", pedido)
					.setParameter("ano", ano)
					.setParameter("mes", mes)
					.getSingleResult();
		} catch(Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public void salvar(TotalAulas totalAulas) {
		/*Frequencia f = get(frequencia.getId().getCodOferta(),
				frequencia.getId().getCodPedido(), 
				frequencia.getId().getAno(),
				frequencia.getId().getMes(), 
				frequencia.getId().getMatricula());*/
		if (totalAulas != null) {
			this.manager.merge(totalAulas);
		}
		
	}

}
